-- CALC_RANDOM_WORDS
-- created by huanghr
-- 计算随机发言

return function(sceneType, marking, wordsIds)

    -- 计算练级工坊的ID
    local function calcWorkshopWordsId(sceneType)

        -- 出现的概率
        local appearChance = math.random(100);
        local sceneChance = RandomWordsM.getSceneRatio(sceneType);
        if appearChance > sceneChance then
            return nil;
        end

        local times = 1;
        local timesChance = math.random(10);
        if timesChance > 7 then
            times = 2;
        end
        local wordsIds = {};
        local posIndex = {1, 2, 3};

        -- 查询上次发言记录
        local oldWordsId = RandomWordsM.getOldWordsId(sceneType);
        if oldWordsId then
            return oldWordsId;
        end
        local appearedWordsId = {};
        for i = 1, times do
            -- 随机当前的位置
            local index = math.random(#posIndex);
            local pos = posIndex[index];
            table.remove(posIndex, index);

            local rangeT = RandomWordsM.calcRangeByScene(sceneType);
            -- 随机获得发言Id
            local function getWordsId()
                local chance = math.random(100);

                local tempId;
                for j = 1, #rangeT do
                    if chance <= rangeT[j].range then
                        tempId = RandomWordsM.getRandomId(sceneType, rangeT[j].type, nil, appearedWordsId);
                    end

                    if tempId ~= nil then
                        return tempId;
                    end
                end
                return tempId;
            end

            local wordsId = getWordsId();

            if wordsId ~= nil then
                table.insert(appearedWordsId, wordsId);
                local ret = {["wordsId"] = wordsId, ["pos"] = pos};
                RandomWordsM.addHaveSaidId(1, wordsId, pos);
                table.insert(wordsIds, ret);
            end
         end
         return wordsIds;
    end

    -- 获得斥候探索的随机ID
     local function calcScoutId(sceneType)
        local i = math.random(100);
        local sceneChance = RandomWordsM.getSceneRatio(sceneType);
            if i > sceneChance then
            return nil;
        else
            -- 获得上次的发言
            local oldWordsId = RandomWordsM.getOldWordsId(sceneType);
            if oldWordsId then
                return oldWordsId;
            end

            -- 获得当前发言
            local chance = math.random(100);

            local rangeT = RandomWordsM.calcRangeByScene(sceneType);
            local wordsId = 1;
            for j = 1, #rangeT do
                if chance <= rangeT[j].range then
                    wordsId = RandomWordsM.getRandomId(sceneType, rangeT[j].type);
                    break;
                end
            end

            -- 记录发言结果
            RandomWordsM.addHaveSaidId(sceneType, wordsId)
            return wordsId;
        end
    end

    -- 计算天赋树冈布奥说的ID
    local function calcSlime(sceneType, petId, wordsIds)
        local i = math.random(100);
        local sceneChance = RandomWordsM.getSceneRatio(sceneType);

        if i > sceneChance then
            return nil;
        else
            -- 查询上次发言的记录
            local oldWordsId = RandomWordsM.getOldWordsId(sceneType, petId);
            if oldWordsId then
               return oldWordsId;
            end

            local rangeT = RandomWordsM.calcRangeByScene(sceneType);

            -- 获得此次发言ID
            local function getWordsId()
                local chance = math.random(100);
                local tempId;
                local retIds = {};

                for j = 1, #rangeT do
                    if rangeT[j].type == 101 then
                        tempId = RandomWordsM.getRandomId(sceneType, rangeT[j].type, petId)
                    else
                        tempId = RandomWordsM.getRandomId(sceneType, rangeT[j].type, nil, wordsIds);
                    end

                    if  tempId ~= nil then
                        table.insert(retIds, tempId);
                    end
                    -- 找到了合适的
                    if tempId and chance <= rangeT[j].range then
                        return tempId;
                    end
                end

                -- 没有找到合适的，在其他选项中随机抽取一个
                if #retIds < 1 then
                    return nil;
                end
                local index = math.random(#retIds);
                local id    = retIds[index];
                return id;
             end

            -- 记录当前冈布奥发言的ID
            local wordsId = getWordsId();

            if wordsId ~= nil then
                RandomWordsM.addHaveSaidId(sceneType, wordsId, petId);
            end

            return wordsId;
        end
    end

    -- 根据当前的场景选择获取发言ID
    local wordsId = nil;
    if sceneType == 1 then
        wordsId = calcWorkshopWordsId(sceneType)
    elseif sceneType == 2 then
        wordsId = calcScoutId(sceneType);
    elseif sceneType == 3 then
        wordsId = calcSlime(sceneType, marking, wordsIds);
    end

    return wordsId;
end